#coding:utf8
import struct, os
import time 
import numpy 

'''
以深发展1997年1月2日的数据为例：
00000000h: 36 B8 30 01 72 06 00 00 86 06 00 00 60 06 00 00 ;
00000010h: 72 06 00 00 77 69 D4 4C 68 FE 66 00 74 06 00 00 ;
以下是分解
00000000h:|36 B8 30 01|72 06 00 00|86 06 00 00|60 06 00 00|;
[36 B8 30 01] = 0x0130B836 = 19970102        日期[unsigned long]
[72 06 00 00] = 0x00000672 = 1650/100 = 16.50    开盘[unsigned long]
[86 06 00 00] = 0x00000686 = 1670/100 = 16.70    最高[unsigned long]
[60 06 00 00] = 0x00000660 = 1632/100 = 16.32    最低[unsigned long]
00000010h:|72 06 00 00|77 69 D4 4C|68 FE 66 00|74 06 00 00|;
[72 06 00 00] = 0x00000672 = 1650/100 = 16.50    收盘[unsigned long]
[77 69 D4 4C] = 0x4CD46977 = 111365048.0    成交额[float]
[68 FE 66 00] = 0x0066FE68 = 6749800        成交量[unsigned long]
[74 06 00 00] = 0x00000674 = 1652/100 = 16.52    上日收盘[unsigned long](保留)
'''

FORMAT = 'IIIIIfIi' 

base_path = os.path.normpath(r'/apps/data/vipdoc') 


  
def trans_day(path, host='172.24.180.42'):
    import os, redis
    r = redis.Redis(host=host)
    
    size = struct.calcsize(FORMAT) 
    
    for filename in os.listdir(path):
        key = filename
        file = open(os.path.join(path,filename),'rb')
        del r[key]
        values = []
        while True:
            buf = file.read(size)
            if not buf: break
            item = struct.unpack(FORMAT, buf) 
            values.append(item)
        
        r.lpush(key, *values)
        
        print '%s completed'%key


def quote_day(code, start=None, end=None):  
    market = code[0:2]
    path = r'%s/%s/lday/%s.day'%(base_path, market, code)
    file = open(path,'rb')
    key = '%s%s.day'%(market, code)
    size = struct.calcsize(FORMAT)  
    res = {}
    if start is None:   
        start = 0
    if end is None:
        end = time.time()
    while True: 
        buf = file.read(size) 
        if not buf: break
        item = struct.unpack(FORMAT, buf)    
        if item[0]>=start and item[0]<=end:
            res[item[0]] = item
    return res
  

def interpolate(m, keys):
    n = len(keys)
    # x, 1, 2, x, 3, 4, x
    #first interpolate leftmost if missing
    i = 0
    while i<n:
        if keys[i] not in m:
            i += 1
        else:
            break
    if i == n:
        raise Error('all missing')
    j = 0
    while j<i:
        m[keys[j]] = m[keys[i]]
        j += 1 
        
    for i in range(n): 
        if keys[i] not in m: 
            m[keys[i]] = m[keys[i-1]] 
        
    
def corr(codes, start=None, end = None):
    qs = []
    for code in codes:
        qs.append(quote_day(code, start, end))
    
    keys = set()
    for q in qs:
        for k,v in q.iteritems():
            keys.add(k)
    
    keys = sorted(keys) 
    
    for q in qs:
        interpolate(q, keys)
    
    for i in range(len(qs)):
        qs[i] = sorted(qs[i].iteritems())
        qs[i] = [p[1][4] for p in qs[i]]
    
    
    return numpy.corrcoef(qs) 
            
codes = ['sh600876','sz000888']   
start, end =  20120501, 20120601

print corr(codes, start,end)  